package com.bestcem.xm.survey.service.reportHandler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @author xiaoshu.shao
 * @date 2023/3/7 11:40
 */
public interface BaseReportHandler {
    Double getMaxScore(JSONObject question, JSONArray scoreRule);
    Double getScore(JSONObject question, Object answers, JSONArray scoreRule, JSONObject scoreConfig);
    List<String> getQuestionTypeValue();

    default JSONObject getScoreGidDict(JSONArray scoreRule) {
        JSONObject scoreGidDict = new JSONObject();
        if (scoreRule == null) {
            return scoreGidDict;
        }
        for (int i = 0; i < scoreRule.size(); i++) {
            JSONObject ele = scoreRule.getJSONObject(i);
            if (ele == null) {
                continue;
            }
            scoreGidDict.put(ele.getString("o_gid"), convertFloat(ele.getDouble("score")));
        }
        return scoreGidDict;
    }


    default JSONObject getMatrixScoreGidDict(JSONArray scoreRule) {
        JSONObject scoreGidDict = new JSONObject();
        if (scoreRule == null) {
            return scoreGidDict;
        }
        for (int i = 0; i < scoreRule.size(); i++) {
            JSONObject ele = scoreRule.getJSONObject(i);
            if (ele == null) {
                continue;
            }
            scoreGidDict.put(ele.getString("r_gid") + "_" + ele.getString("o_gid"), convertFloat(ele.getDouble("score")));
        }
        return scoreGidDict;
    }

    default double convertFloat(Double value) {
        if (value == null) {
            return 0d;
        }
        BigDecimal bigDecimal = new BigDecimal(value);
        return bigDecimal.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros().doubleValue();
    }
}
